Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
Entityframework > Get tracked entity by key (different reference)

I'm in need of the functionality to retrieve a tracked entity by key without a SQL call to the DB. So I need something like the .Find but if it's not tracked NULL is returned.

The reason why I need this is because following code throws:

public void Test()
{
    var id = 5;

    var a = new Entity { Id = id };
    _ctx.Entry(a).State = EntityState.Added;

    var b = new Entity { Id = id };
    var bEntry = _ctx.Entry(b);

    // Delete entity
    // When it was already added in this 'lifetime' detach it again (since it's not saved in the DB yet)
    if (bEntry.State == EntityState.Added)
        bEntry.State = EntityState.Detached;
    else
        bEntry.State = EntityState.Deleted;
    
    var c = new Entity { Id = id };
    var cEntry = _ctx.Entry(c);
    
    // Add entity
    // When it was already deleted in this 'lifetime' detach it again (since it's not removed in the DB yet)
    if (cEntry.State == EntityState.Deleted)
        cEntry.State = EntityState.Detached;
    else
        cEntry.State = EntityState.Added;
    
    var d = new Entity { Id = id };
    var dEntry = _ctx.Entry(d);
}

If you would never create new entities but always use a. This code will work. The reason why it doesn't work now, is because internally EF tracks the entities in a dictionary using a 'by ref compare' (https://github.com/dotnet/efcore/blob/9ac01d6035c76626d89aa1a3cd8d200db2c3c0e1/src/EFCore/ChangeTracking/Internal/EntityReferenceMap.cs#L103)

As a result the TryGetValue EF uses will be false and it will create a new entry with state Detached. And later the code will crash because I try to add a entity that is already tracked.

This above problem would be solved if I could get the tracked entity since they would have the same ref. But I don't seem to find a way to access the same code that happens in .Find(). I could in theory use the .Local property or get it by using ChangeTracker.Entries() but since this are just regular lists the performance to lookup the entry is taking way to long.

I know this is a very specific question. But I don't really seem to find anything. It's a bit a shame to give up since internally EF knows what I need.

Brecht

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda